package org.jetnuke.modules.download;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.DownloadDownloadDAO;

public class Top implements IAction {

	int ratenum=25;
	int downvotemin=5;//TODO
	String ratetype="num";
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		int topdownspercentrigger=0;
		int topdowns = ratenum;
		int topdownspercent = topdowns;
    	if (ratetype.equals("percent")) {
    		topdownspercentrigger = 1;
    	}
    	DownloadDownloadDAO ddm=DataAccessDriver.getInst().newDownloadDownloadDAO();
    	if(topdownspercentrigger==1){
    		
    		int totalrateddowns=ddm.countWithRating();
    			//downloads_downloadsPeer.count(new Criteria().add(downloads_downloadsPeer.DOWNLOADRATINGSUMMARY,0d,Criteria.GREATER_THAN));
        	topdowns = (int)(1.*totalrateddowns * topdowns/100);
        	vc.put("totalrateddowns",new Integer(totalrateddowns));
    	}
	    vc.put("topdownspercentrigger",new Integer(topdownspercentrigger));
	    vc.put("topdowns",new Integer(topdowns));
	    vc.put("downvotemin",new Integer(downvotemin));
	    vc.put("topdownspercent",new Integer(topdownspercent));
	    List ls=ddm.selectWithRating(downvotemin,topdowns);
	    	//downloads_downloadsPeer.doSelect(new Criteria().add(downloads_downloadsPeer.DOWNLOADRATINGSUMMARY,0d,Criteria.GREATER_THAN)
	    		//.add(downloads_downloadsPeer.TOTALVOTES,downvotemin,Criteria.GREATER_EQUAL)
				//.addDescendingOrderByColumn(downloads_downloadsPeer.DOWNLOADRATINGSUMMARY)
				//.setLimit(topdowns));
	    vc.put("downs",ls);
	    
		return new ActionForward(op, info);
	}

	public void setRatenum(int rateNum) {
		this.ratenum = rateNum;
	}
	public void setRatetype(String ratetype) {
		this.ratetype = ratetype;
	}

}
